package jm.demo.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
class TrackExceptions {

    // Detect after throwing advice to catch methods that throw exceptions:
    @Pointcut("execution(* *(..))  && within(AspectjDemo)")
    void interestingMethods() {
    }

    @AfterThrowing(pointcut = "interestingMethods()", throwing = "t")
    public void logException(JoinPoint thisJoinPoint, Throwable t) {
        System.err.println("Exception thrown at "
                + thisJoinPoint.getSourceLocation() + ".  Exception was " + t);
    }

    //
    @Before("handler(Throwable+) && args(t) && within(AspectjDemo)")
    public void watchHandler(JoinPoint thisJoinPoint, Throwable t) {
        System.err.println("Entered exception handler at "
                + thisJoinPoint.getSourceLocation() + ".  Exception was " + t);
    }

}